package ai.fabu.dataplatform.service.GRPCServer;

import ai.fabu.dataplatform.SaveResponse;
import ai.fabu.dataplatform.constant.ResultCode;
import ai.fabu.dataplatform.help.Result;
import io.grpc.Status;
import io.grpc.stub.StreamObserver;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class FunctionUtil {
//    public static void responseUpdateData(Result<Long> result, StreamObserver<SaveResponse> responseObserver){
//        if(result.getCode() == ResultCode.SUCCESS.getCode()){
//            SaveResponse response = SaveResponse.newBuilder().setUpdateNum(result.getData()).build();
//            responseObserver.onNext(response);
//            responseObserver.onCompleted();
//        }
//        else{
//            log.error("error code: {}, error message: {}", result.getCode(), result.getMsg());
//            responseObserver.onError(Status.UNKNOWN.withDescription(result.getMsg()).asRuntimeException());
//        }
//    }

    public static void errorHandler(StreamObserver<?> responseObserver, String functionName, String msg){
        log.error("{}: {}", functionName, msg);
        responseObserver.onError(Status.ABORTED.withDescription(msg).asRuntimeException());
    }

    public static void errorHandler(StreamObserver<?> responseObserver, String functionName, String msg, Throwable throwable){
        log.error("{}: {}", functionName, msg);
        responseObserver.onError(throwable);
    }

    public static void errorHandler(Result result, StreamObserver<?> responseObserver, String functionName){
        log.error("{}: error code: {}, error message: {}", functionName, result.getCode(), result.getMsg());
        responseObserver.onError(Status.UNKNOWN.withDescription(result.getMsg()).asRuntimeException());
    }
}
